<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 8:36
 */
namespace app\admin\controller;


use think\Db;
use think\Request;
class About extends Base {
    /**
     * 关于我们（logo，功能介绍，法律声明）
     * @return mixed|\think\response\Json
     */
    public function about(){
        if(Request::instance()->isPost()){
            extract(input());
            Db::table('up_about')->where('id', 3)->update(['content' => $content3]);
            Db::table('up_about')->where('id', 4)->update(['content' => $content4]);
            Db::table('up_about')->where('id', 5)->update(['content' => $content5]);
            Db::table('up_about')->where('id', 9)->update(['content' => $content9]);
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        $about_content3 = Db::table('up_about')->where('id', 3)->value('content');
        $about_content4 = Db::table('up_about')->where('id', 4)->value('content');
        $about_content5 = Db::table('up_about')->where('id', 5)->value('content');
        $about_content9 = Db::table('up_about')->where('id', 9)->value('content');
        $this->assign('content3', $about_content3);
        $this->assign('content4', $about_content4);
        $this->assign('content5', $about_content5);
        $this->assign('content9', $about_content9);
        return $this->fetch('about/about');
    }

    /**
     * 隐私协议
     * @return mixed|\think\response\Json
     */

    public function yinsi(){
        if(Request::instance()->isPost()){
            extract(input());
            $res = Db::table('up_about')->where('id', 2)->update(['content' => $content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 2)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/yinsi');
    }
    /**
     * 用户协议
     * @return mixed|\think\response\Json
     */
    public function xieyi(){
        if(Request::instance()->isPost()){
            extract(input());
            $res = Db::table('up_about')->where('id', 1)->update(['content' => $content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $content = Db::table('up_about')->where('id', 1)->value('content');
        $this->assign('content', $content);
        return $this->fetch('about/xieyi');
    }
    /**
     * 签到相关配置
     * @return mixed|\think\response\Json
     */
    public function signin(){
        if(Request::instance()->isPost()){
            extract(input());
            Db::table('up_about')->where('id', 6)->update(['content' => $content6]);
            Db::table('up_about')->where('id', 7)->update(['content' => $content7]);
            Db::table('up_about')->where('id', 8)->update(['content' => $content8]);
            return json(['code' => 200, 'msg' => '保存成功']);
        }
        $about_content6 = Db::table('up_about')->where('id', 6)->value('content');
        $about_content7 = Db::table('up_about')->where('id', 7)->value('content');
        $about_content8 = Db::table('up_about')->where('id', 8)->value('content');
        $this->assign('content6', $about_content6);
        $this->assign('content7', $about_content7);
        $this->assign('content8', $about_content8);
        return $this->fetch('about/signin');
    }
    //--------------------------------------------------------------------------------------
    //----------------------------意见反馈----------------------------------------------
    //--------------------------------------------------------------------------------------
    /**
     * 意见反馈列表
     * @return mixed|\think\response\Json
     */
    public function feedback(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone|feedback_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($feedback_status)&&$feedback_status!="")
            {
                $map['feedback_status'] = $feedback_status;
            }

            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.feedback_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.feedback_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.feedback_time'] = ['between time',[$start,$end]];
            }
            $count = Db::table('up_feedback')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_feedback')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->field('uuu.user_username,uuu.user_phone,a.*')
                ->order('feedback_time desc')
                ->page($page, $limit)
                ->select();//计算总页面

            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }
    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function feedhandle(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'feedback_status' => 2,
                'feedback_handle_content' => $content,
                'feedback_handle_time' => $today
            ];
            $res = Db::table('up_feedback')->where('feedback_id', $feedback_id)->update($update);
            if($res){
                $f_user_id = Db::table('up_feedback')->where('feedback_id', $feedback_id)->value('f_user_id');
                $data = [
                    'n_title' => '反馈回复通知',
                    'n_content' => '回复内容：' . $content . '，感谢您的反馈',
                    'n_app_id' => 0,
                    'n_type' => 1,
                    'n_range' => 4,//推送范围  4个别
                    'n_user_id' => ",{$f_user_id},", // 推送用户
                    'n_read_user_id' => ',0,', // 已读用户
                    'n_del_user_id' => ',0,', // 删除的用户
                    'n_send_datetime' => $today, // 推送时间
                ];
                Db::table('up_notice')->insert($data);
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $feedback_id = input('feedback_id');
        $info = Db::table('up_feedback')->where('feedback_id', $feedback_id)->find();
        $this->assign([
            'feedback_id' => $feedback_id,
            'info' => $info,
        ]);
        return $this->fetch();
    }
    /**
     * 备注
     * @return mixed|\think\response\Json
     */
    public function feedremark(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'feedback_remark' => $content,
            ];
            Db::table('up_feedback')->where('feedback_id', $feedback_id)->update($update);
            return json(['code' => 200, 'msg' => '操作成功']);
        }
        $feedback_id = input('feedback_id');
        $info = Db::table('up_feedback')->where('feedback_id', $feedback_id)->find();
        $this->assign([
            'feedback_id' => $feedback_id,
            'info' => $info,
        ]);
        return $this->fetch();
    }
    /**
     * 删除反馈
     * @return \think\response\Json
     */
    public function feeddel(){
        $id = input('id');
        $res = Db::table('up_feedback')->where('feedback_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    //--------------------------------------------------------------------------------------
    //----------------------------常见问题----------------------------------------------
    //--------------------------------------------------------------------------------------
    /**
     * 问题列表
     * @return mixed|\think\response\Json
     */
    public function question(){
        if(request()->isAjax ()){
            extract(input());
            $where = [];
            if(isset($keyword)&&$keyword!="")
            {
                $where['q_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $where['q_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $where['q_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $where['q_time'] = ['between time',[$start,$end]];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('question')->where($where)->count();// 获取总条数
            $lists = Db::name('question')
                ->where($where)
                ->field('q_content',true)
                ->page($Nowpage,$limits)
                ->order('q_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加问题
     * @return mixed|\think\response\Json
     */
    public function quesadd(){
        if(Request::instance()->isPost()){
            $q_title = trim(input('q_title'), ' ');
            $q_content = trim(input('q_content'), ' ');
            $data = [
                'q_title' => $q_title,
                'q_content' => $q_content,
                'q_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_question')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    /**
     * 编辑问题
     * @return mixed|\think\response\Json
     */
    public function quesedit(){
        if(Request::instance()->isPost()){
            $q_id = input('q_id');
            $q_title = trim(input('q_title'), ' ');
            $q_content = trim(input('q_content'), ' ');
            $data = [
                'q_title' => $q_title,
                'q_content' => $q_content,
                'q_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_question')->where('q_id', $q_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $q_id = input('q_id');
        $info = Db::table('up_question')
            ->where('q_id', $q_id)
            ->find();
        $this->assign([
            'q_id' => $q_id,
            'info' => $info
        ]);
        return $this->fetch();
    }
    /**
     * 删除问题
     * @return \think\response\Json
     */
    public function quesdel(){
        $id = input('id');
        $res = Db::table('up_question')->where('q_id',$id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}